using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Wax;

namespace WaxLibTest
{
    [TestFixture]
    public class DialogHandlerCommandTest
    {
        [Test]
        public void ParseTest()
        {
            DialogHandlerCommand dialogCommand = (DialogHandlerCommand)CommandFactory.Parse(new String[] { "CmdSimpleDialogHandler", "AttachDialogHandler", "SimpleJavaDialog,dlgSimple", string.Empty, string.Empty });

            Assert.AreEqual("CmdSimpleDialogHandler", dialogCommand.Id);
            Assert.AreEqual(CommandType.AttachDialogHandler, dialogCommand.CommandType);
            Assert.AreEqual(DialogHandlerType.SimpleJavaDialog, dialogCommand.DialogType);
            Assert.AreEqual("dlgSimple", dialogCommand.DialogId);
        }

        [Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void IncorrectDiaglogTypeSpecified()
        {
            DialogHandlerCommand dialogCommand = (DialogHandlerCommand)CommandFactory.Parse(new String[] { "CmdSimpleDialogHandler", "AttachDialogHandler", "UnknownDialog,dlgSimple", string.Empty, string.Empty });
        }

        [Test]
        public void AttachDialogHandlerTest()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "CmdSimpleDialogHandler", "AttachDialogHandler", "SimpleJavaDialog,dlgSimple", string.Empty, string.Empty }));
                testManager.Execute();

                Assert.AreEqual(1, testManager.Browser.DialogWatcher.Count);
            }
        }

        [Test]
        public void AttachConfirmDialogHandlerTest()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "CmdConfirmDialogHandler", "AttachDialogHandler", "ConfirmDialog,dlgConfirm", string.Empty, string.Empty }));
                testManager.Execute();

                Assert.AreEqual(1, testManager.Browser.DialogWatcher.Count);
            }
        }

        [Test]
        public void DialogMessageTest()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "CmdStart", "BringToFront", string.Empty, string.Empty, string.Empty }));
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "CmdSimpleDialogHandler", "AttachDialogHandler", "SimpleJavaDialog,dlgSimple", string.Empty, string.Empty }));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "CmdClickLink", "Click", string.Empty, "ById", "linkAlert" }));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "CmdSimpleDialogHandler2", "GetJavaDialogMessage", "SimpleJavaDialog,dlgSimple", string.Empty, string.Empty }));
                testManager.Execute();

                Assert.AreEqual("This is an alert message", testManager.ExecutionContext.LastFoundValue);
            }
        }

        [Test, ExpectedException(typeof(ExecutionException))]
        public void ExpectedErrorWhenUsingADialogBeforeAttachingIt()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "CmdSimpleDialogHandler2", "GetJavaDialogMessage", "SimpleJavaDialog,dlgSimple", string.Empty, string.Empty }));
                testManager.Execute();
            }
        }
    }
}
